package com.ruicar.afs.cloud.batch.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.batch.entity.SysHolidayInfo;
import com.ruicar.afs.cloud.batch.mapper.SysHolidayInfoMapper;
import com.ruicar.afs.cloud.batch.service.AfsSysHolidayService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.data.datasource.annotation.DS;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
public class AfsSysHolidayServiceImpl extends ServiceImpl<SysHolidayInfoMapper, SysHolidayInfo> implements AfsSysHolidayService {
    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    public boolean isHoliday(String bhDate) {
        Date date = DateUtil.parse(bhDate, DatePattern.PURE_DATE_PATTERN);
        int day = DateUtil.calendar(date).get(Calendar.DAY_OF_YEAR)-1;
        SysHolidayInfo sysHolidayInfo = this.getOne(Wrappers.<SysHolidayInfo>lambdaQuery().eq(SysHolidayInfo::getYear,bhDate.substring(0,4)),false);
        if(sysHolidayInfo==null){
            throw new RuntimeException("节假日未维护");
        }else {
            return '1'==sysHolidayInfo.getHolidayInfo().charAt(day);
        }
    }

    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    public String nextWorkDay(String bhDate) {
        Date date = DateUtil.parse(bhDate, DatePattern.PURE_DATE_PATTERN);
        int day = DateUtil.calendar(date).get(Calendar.DAY_OF_YEAR)-1;
        SysHolidayInfo sysHolidayInfo = this.getOne(Wrappers.<SysHolidayInfo>lambdaQuery().eq(SysHolidayInfo::getYear,bhDate.substring(0,4)),false);
        if(sysHolidayInfo==null){
            throw new RuntimeException("节假日未维护");
        }else {
           int pos  = sysHolidayInfo.getHolidayInfo().indexOf("0",day+1);
           if(pos==-1){//当年节假日维护未找到下一个工作日，需从下一年获取
               Calendar calendar = DateUtil.calendar(date);
               calendar.add(Calendar.YEAR,1);
               sysHolidayInfo = this.getOne(Wrappers.<SysHolidayInfo>lambdaQuery().eq(SysHolidayInfo::getYear,DateUtil.format(calendar.getTime(),"yyyy")),false);
               if(sysHolidayInfo==null){
                   throw new RuntimeException(calendar.get(Calendar.YEAR)+"节假日未维护");
               }else {
                   pos  = sysHolidayInfo.getHolidayInfo().indexOf("0");
                   if(pos==-1){
                       throw new RuntimeException(calendar.get(Calendar.YEAR)+"节假日维护错误");
                   }else {
                        calendar.set(Calendar.DAY_OF_YEAR,pos+1);
                        return DateUtil.format(calendar.getTime(),DatePattern.PURE_DATE_PATTERN);
                   }
               }
           }else{
               Calendar calendar = DateUtil.calendar(date);
               calendar.set(Calendar.DAY_OF_YEAR,pos+1);
               return DateUtil.format(calendar.getTime(),DatePattern.PURE_DATE_PATTERN);
           }
        }
    }
}
